﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WarehouseManagement.BusinessLogic.DTO;
using WarehouseManagement.DataAccess;


namespace WarehouseManagement.BusinessLogic.BC
{
    public class MerchandiseBC : BaseBC
    {

        public MerchandiseBC()
            : base()
        {

        }

        public IEnumerable<Merchandise> GetMerchandiseList()
        {
            return Context.Merchandises.AsEnumerable();
        }

        public IEnumerable<MerchandiseDTO> GetMerchandiseDTOList()
        {
            var list = GetMerchandiseList().Select(e => e.ToDTO());
            return list;

        }
        public Merchandise GetMerchandiseById(int id)
        {
            return Context.Merchandises.SingleOrDefault(e => e.Id == id);
        }
        //public IEnumerable<ExtraMerchandiseDTO> GetMerchandiseListWithExtraInfo()
        //{
        //    //var list = GetMerchandiseList();

        //    //foreach (var item in list)
        //    //{
        //    //    var e = new ExtraMerchandiseDTO()
        //    //    {
        //    //        Id = item.Id,
        //    //        Madein = item.Madein,
        //    //        MerchandiseName = item.MerchandiseName,
        //    //        Unit = item.Unit,
        //    //        TradeMark = item.TradeMark,
        //    //    };

        //    //    e.QuantityInStock = _repo.GetQuantityInStock(e.Id, DateTime.Now.Date).Quantity;
        //    //    e.OrderedQuantity = _orderRepo.GetPendingOrderedMerchandiseCount(e.Id);
        //    //    yield return e;

        //    //}

        //    yield return null;

        //}

        public void AddMerchandise(MerchandiseDTO dto)
        {
            var entity = dto.ToEntity();
            Context.Merchandises.InsertOnSubmit(entity);
            SubmitChanges();
        }

        public void DeleteMerchandises(IEnumerable<int> idList)
        {
            foreach (int id in idList)
                Context.Merchandises.DeleteOnSubmit(GetMerchandiseById(id));
            SubmitChanges();
        }

        public void UpdateMerchandise(int id, MerchandiseDTO newDto)
        {
            var entity = GetMerchandiseById(id);
            if (entity == null) return;

            if (newDto.CategoryId.HasValue)
                entity.MerchandiseCategory = Context.MerchandiseCategories.SingleOrDefault(e => e.Id == newDto.CategoryId);
            entity.Madein = newDto.Madein;
            entity.MerchandiseName = newDto.MerchandiseName;
            entity.TradeMark = newDto.TradeMark;
            entity.Unit = newDto.Unit;

            SubmitChanges();
        }

        //public double GetQuantityInStock(int merchandiseId, DateTime from)
        //{
        //    var list = Context.MerchandiseInStocks.Where(
        //        e => e.MerchandiseId == merchandiseId
        //            && e.Date.Date <= from.Date)
        //            .OrderByDescending(e => e.Date)
        //            .ToList();

        //    if (list.Count() == 0)
        //        return 0;

        //    var firstItem = list.FirstOrDefault();
        //    if (firstItem == null)
        //        return 0;
        //    return firstItem.QuantityInStock;
        //}

        //public MerchandiseInStock GetMerchandiseInStock(int merchandiseId, DateTime from)
        //{
        //    MerchandiseInStock result = new MerchandiseInStock();
        //    var list = Context.MerchandiseInStocks.Where(
        //        e => e.MerchandiseId == merchandiseId
        //            && e.Date.Date <= from.Date)
        //            .OrderByDescending(e => e.Date)
        //            .ToList();

        //    if (list.Count() > 0)
        //    {
        //        var firstItem = list.FirstOrDefault();
        //        if (firstItem != null)
        //            result = firstItem;
        //    }

        //    return result;
        //}

        //public void AppendMerchandiseInStock(int merchandiseId, DateTime fromDate, double quantity, decimal value)
        //{
        //    var previousItem = GetMerchandiseInStock(merchandiseId, fromDate.Date);

        //    if (Context.MerchandiseInStocks.Any(e => e.Date.Date == fromDate.Date
        //        && e.MerchandiseId == merchandiseId))
        //    {

        //        var entity = Context.MerchandiseInStocks.SingleOrDefault(e => e.Date == fromDate.Date && e.MerchandiseId == merchandiseId);
        //        entity.QuantityInStock = previousItem.QuantityInStock + quantity;
        //        entity.ValueInStock = previousItem.ValueInStock + value;
        //    }
        //    else
        //    {
        //        Context.MerchandiseInStocks.InsertOnSubmit(new MerchandiseInStock()
        //        {
        //            Id = Guid.NewGuid(),
        //            Date = fromDate,
        //            MerchandiseId = merchandiseId,
        //            ValueInStock = previousItem.ValueInStock + value,
        //            QuantityInStock = previousItem.QuantityInStock + quantity
        //        });
        //    }
        //    SubmitChanges();
        //}

        //public void UpdateAllMerchandisesInStockAfterDate(int merchandiseId, DateTime fromDate, double quantity, decimal value)
        //{
        //    var list = Context.MerchandiseInStocks.Where(e => e.Date.Date > fromDate.Date && e.MerchandiseId == merchandiseId)
        //        .AsEnumerable();
        //    foreach (var item in list)
        //    {
        //        AppendMerchandiseInStock(item.MerchandiseId, item.Date, quantity, value);
        //    }
        //}





        //public double GetOrderedQuantity(int merchandiseId)
        //{

        //    var list = Context.OrderDetails
        //        .Where(e => e.MerchandiseId == merchandiseId && e.Order.ActualShippingDate == null);
        //    if (list.Count() > 0)
        //        return list.Sum(e => e.Quantity);
        //    return 0;
        //}

        public IEnumerable<MerchandiseCategoryDTO> GetCategoryList()
        {
            return Context.MerchandiseCategories.Select(e => e.ToDTO());
        }

        public void AddCategory(MerchandiseCategoryDTO dto)
        {
            var entity = dto.ToEntity();
            Context.MerchandiseCategories.InsertOnSubmit(entity);
            SubmitChanges();
        }

        public void UpdateCategory(MerchandiseCategoryDTO dto)
        {
            var entity = Context.MerchandiseCategories.SingleOrDefault(e => e.Id == dto.Id);
            entity.MerchandiseCategoryName = dto.MerchandiseCategoryName;
            entity.Notes = dto.Notes;
            SubmitChanges();
        }
        public void DeleteCategory(int categoryId)
        {
            var entity = Context.MerchandiseCategories.SingleOrDefault(e => e.Id == categoryId);
            if (entity != null)
            {
                Context.MerchandiseCategories.DeleteOnSubmit(entity);
                SubmitChanges();
            }
        }

        public bool ContainCategory(string name)
        {
            return Context.MerchandiseCategories.Any(e => e.MerchandiseCategoryName == name);
        }
    }
}
